//
//  MeUserHeaderView.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/7.
//

import UIKit

class MeUserHeaderView: UIView {
    
    lazy var headerBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.imageView?.contentMode = .scaleAspectFill
        btn.rx.tap.bind{ [weak self] in
            self?.gotoEditVC()
        }.disposed(by: rx.disposeBag)
        btn.wn_cornerWithBorder(33.5)
        return btn
    }()
    
    lazy var vipImage: UIImageView = {
        let image = UIImageView(image: UIImage(named: "setting_sub_vip"))
        image.isHidden = true
        return image
    }()
    
    lazy var nameBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitleColor(.black, for: .normal)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 24)
        btn.titleLabel?.lineBreakMode = .byTruncatingTail
        btn.titleLabel?.textAlignment = .left
        btn.rx.tap.bind{ [weak self] in
            self?.gotoEditVC()
        }.disposed(by: rx.disposeBag)
        return btn
    }()
    
    lazy var storeView: MeUserTopView = {
        let view = MeUserTopView()
        view.setTopType(0)
        return view
    }()
    
    lazy var goldView: MeUserTopView = {
        let view = MeUserTopView()
        view.setTopType(1)
        return view
    }()
        
    var model: UserDetailModel? {
        didSet {
            if let m = model {
                headerBtn.wn_setupImg(m.avatar, placeHolder: "placeholder_header")
                vipImage.isHidden = m.isVip == 0
                nameBtn.setTitle(m.realName, for: .normal)
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        addSubview(headerBtn)
        addSubview(vipImage)
        addSubview(nameBtn)
        addSubview(storeView)
        addSubview(goldView)
        headerBtn.snp.makeConstraints { make in
            make.top.equalTo(8)
            make.left.equalTo(15)
            make.width.height.equalTo(67)
        }
        vipImage.snp.makeConstraints { make in
            make.top.equalTo(headerBtn.snp.top).offset(-12)
            make.left.equalTo(headerBtn.snp.left).offset(-12)
            make.width.height.equalTo(32)
        }
        nameBtn.snp.makeConstraints { make in
            make.centerY.equalTo(headerBtn.snp.centerY)
            make.left.equalTo(headerBtn.snp.right).offset(12)
            make.width.lessThanOrEqualTo(ScreenW-100)
        }
        storeView.snp.makeConstraints { make in
            make.top.equalTo(headerBtn.snp.bottom).offset(23)
            make.left.equalTo((ScreenW-scaleWidth(164)*2)*0.33)
            make.size.equalTo(CGSize(width: scaleWidth(164), height: 80))
        }
        goldView.snp.makeConstraints { make in
            make.centerY.equalTo(storeView.snp.centerY)
            make.right.equalTo(-12)
            make.size.equalTo(storeView.snp.size)
        }
        storeView.wn_cornerWithBorder(10, 0.1, .white, 1, .gray, .init(width: 0, height: 1), 1, 0.5)
        goldView.wn_cornerWithBorder(10, 0.1, .white, 1, .gray, .init(width: 0, height: 1), 1, 0.5)
        storeView.layer.masksToBounds = false
        goldView.layer.masksToBounds = false
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func gotoEditVC() {
        let vc = WNUserInfoVC()
        vc.hidesBottomBarWhenPushed = true
        viewContainingController?.navigationController?.pushViewController(vc, animated: true)
    }
}
